package com.Vronsky.core.connection;

import com.Vronsky.core.dataSource.SimpleDataSource;
import com.Vronsky.core.factory.SimpleDataSourceFactory;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.Queue;

/**
 * 对Connection对象进行装饰
 */

/**
 * 实现com.Vronsky.core.connection.Connection接口的作用
 * 用来迷惑用户，让他们以为获取的是一个Connection，实际是DecorateConnection
 */
public class DecorateConnection implements com.Vronsky.core.connection.Connection {
//    private Boolean inUse;
    //原生连接对象Connection
    private Connection connection;





    public Connection getConnection() {
        return connection;
    }

    public void setConnection(Connection connection) {
        this.connection = connection;
    }


    public DecorateConnection(Connection connection) {
        this.connection = connection;
    }

    public DecorateConnection() {
    }

    @Override
    public void close(){
        recoveryConn();
    }

    //回收连接
    private void recoveryConn(){
        SimpleDataSource simpleDataSource = SimpleDataSourceFactory.getSimpleDataSource();
        Queue<DecorateConnection> connPool = simpleDataSource.getConnPool();
        connPool.add(this);
        synchronized (simpleDataSource){
            simpleDataSource.notifyAll();
        }
    }
}
